package ru.cod.rs.managers;

import java.util.ArrayList;
import java.util.List;

import ru.cod.rs.enums.Buildings;
import ru.cod.rs.enums.Direction;
import ru.cod.rs.enums.Sides;
import ru.cod.rs.enums.Units;
import ru.cod.rs.objects.MoveObject;
import ru.cod.rs.objects.SideObject;
import ru.cod.rs.types.Speed;
import ru.cod.rs.views.GameView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
 * Manager
 * 
 * @author Suslov Vadim
 * 
 */
public class GameManager extends DrawManager {

	private List<SideObject> buildings;
	private List<MoveObject> units;

	public GameManager(Context c, GameView v) {
		super(c, v);
		buildings = new ArrayList<SideObject>();
		units=new ArrayList<MoveObject>();
		pushBuilding(Buildings.EXAMPLE, Sides.ALLY);
		pushBuilding(Buildings.EXAMPLE, Sides.ENEMY);
		pushUnit(Units.EXAMPLE, Sides.ALLY);
		pushUnit(Units.EXAMPLE,Sides.ENEMY);
		
	}

	// //////////////////////////////////////

	// ///////////////////////////////////////
	@Override
	protected void update() {
		for(MoveObject unit:units) {
			unit.update();
		}
	};

	@Override
	protected void render(Canvas c) {
		c.drawColor(Color.GRAY);
		drawText(c);
		drawBuildings(c);
		drawUnits(c);
	}

	// /////////////////////////////////////////////

	private void drawText(Canvas canvas) {
		Paint p = new Paint();
		p.setColor(Color.GREEN);
		canvas.drawText(String.valueOf(view.getWidth()), view.getWidth() - 25,
				15, p);
	}

	private void drawBuildings(Canvas canvas) {
		if (!buildings.isEmpty()) {
			for (SideObject building : buildings) {
				building.draw(canvas);
			}
		}
	}
	
	private void drawUnits(Canvas canvas) {
		if(!units.isEmpty()) {
			for(MoveObject unit:units) {
				unit.draw(canvas);
			}
		}
	}

	// /////////////////////////////////////////////

	private void pushBuilding(Buildings b, Sides s) {
		SideObject building = new SideObject();
		Bitmap bitmap = getImage(b.bitmapId());

		building.setImage(bitmap);

		int x = 0, y = view.getHeight() - building.getHeight();

		if (Sides.ENEMY.equals(s)) {
			x = view.getWidth() - building.getWidth();
		}
		building.setSide(s);
		building.setX(x);
		building.setY(y);
		buildings.add(building);
	}
	
	private void pushUnit(Units u, Sides s) {
		MoveObject unit = new MoveObject();
		Bitmap bitmap = getImage(u.bitmapId());
		
		unit.setImage(bitmap);
		Speed speed=new Speed(1,Direction.RIGHT);
		int x=0,y=view.getHeight()-unit.getHeight();
		
		if(Sides.ENEMY.equals(s)) {
			x=view.getWidth()-unit.getWidth();
			speed.setDirection(Direction.LEFT);
		}
		unit.setSpeed(speed);
		unit.setSide(s);
		unit.setX(x);
		unit.setY(y);
		units.add(unit);
	}

}
